<template>
  <div id="warningSet">
    <h3 class="warnSet_title">预警开关</h3>
    <ul class="warnSet_filter" style="height:60px">
      <li class="warnSet_filter_item warning">
        <span class="warnSet_filter_item_title">预警开关：</span>
        <el-radio
          v-model="warnData.switch"
          label="1"
        >打开</el-radio>
        <el-radio
          v-model="warnData.switch"
          label="0"
        >关闭</el-radio>
      </li>
    </ul>
    <h3 class="warnSet_title">条件设置</h3>
    <ul class="warnSet_filter" style="height:100px">
      <li class="warnSet_filter_item">
        <span class="warnSet_filter_item_title">情感选择：</span>
        <el-radio
          v-model="warnData.emotion"
          label="1"
        >全部</el-radio>
        <el-radio
          v-model="warnData.emotion"
          label="2"
        >正面</el-radio>
        <el-radio
          v-model="warnData.emotion"
          label="4"
        >中立</el-radio>
        <el-radio
          v-model="warnData.emotion"
          label="3"
        >负面</el-radio>
      </li>
      <li class="warnSet_filter_item">
        <span class="warnSet_filter_item_title">来源类型：</span>
        <el-checkbox
          :indeterminate="warnData.isIndeterminate"
          v-model="warnData.checkAll"
          @change="handleCheckAllChange"
        >全选</el-checkbox>
        <el-checkbox-group
          v-model="warnData.checkedTypeResource"
          @change="handleCheckedChange"
        >
          <el-checkbox
            v-for="(item) in warnSetData.typeResourceList"
            :label="item.value"
            :value="item.value"
            :key="item.value"
          >{{item.label}}</el-checkbox>
        </el-checkbox-group>
      </li>
    </ul>
    <h3 class="warnSet_title">时间设置</h3>
    <ul class="warnSet_filter" style="height:120px">
      <li class="warnSet_filter_item timeInterval singular">
        <span class="warnSet_filter_item_title">预警间隔：</span>
        <el-radio v-model="warnData.warnTimeInterval" label="1">实时预警</el-radio>
        <el-radio v-model="warnData.warnTimeInterval" label="2">30分钟</el-radio>
        <el-radio v-model="warnData.warnTimeInterval" label="3">1小时</el-radio>
        <el-radio v-model="warnData.warnTimeInterval" label="4">2小时</el-radio>
        <el-radio v-model="warnData.warnTimeInterval" label="5">3小时</el-radio>
        <el-radio v-model="warnData.warnTimeInterval" label="6">6小时</el-radio>
      </li>
      <li class="warnSet_filter_item timeInterval">
        <span class="warnSet_filter_item_title">监测时间：</span>
        <el-time-picker
            is-range
            v-model="warnData.timePicker"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围">
        </el-time-picker>
      </li>
      <li class="warnSet_filter_item timeInterval singular">
        <span class="warnSet_filter_item_title">来源类型：</span>
        <el-radio v-model="warnData.weekend" label="0">周末预警</el-radio>
        <el-radio v-model="warnData.weekend" label="1">周末不预警</el-radio>
      </li>
    </ul>
    <h3 class="warnSet_title">推送设置</h3>
    <ul class="warnSet_filter" style="height:60px">
      <li class="warnSet_filter_item warning">
        <span class="warnSet_filter_item_title">微信推送：</span>
        <el-radio
          v-model="warnData.chat"
          label="0"
        >打开</el-radio>
        <el-radio
          v-model="warnData.chat"
          label="1"
        >关闭</el-radio>
      </li>
    </ul>
    <el-button class="saveBtn" @click="updateWarnSet">保存</el-button>
  </div>
</template>
<script>
export default {
  props: ['warnSetData'],
  data() {
    return {
      warnData: {
        switch: "1",
        emotion: "1",
        checkAll: false,
        checkedTypeResource: [],
        isIndeterminate: false,
        warnTimeInterval: '1',
        timePicker: null,
        weekend: '1',
        chat: '0'
      }
    };
  },
  methods:{
      updateWarnSet(){
        let dataFromType = '';
        for (let i = 0; i < this.warnData.checkedTypeResource.length; i++) {
          if(i == this.warnData.checkedTypeResource.length -1){
            dataFromType = dataFromType + this.warnData.checkedTypeResource[i];
          }else{
            dataFromType = dataFromType + this.warnData.checkedTypeResource[i] + ',';
          }
        }
        let param = {};
        param.id = parseInt(this.warnSetData.caseId);
        param.userId = parseInt(this.$store.state.user.id);
        param.warnSwitch = parseInt(this.warnData.switch);
        param.emotionType = parseInt(this.warnData.emotion);
        param.dataFromType = dataFromType;
        param.warnTimeType = parseInt(this.warnData.warnTimeInterval);
        let warnBeginTime = '';
        let warnEndTime = '';
        if(this.warnData.timePicker){
          warnBeginTime = (new Date(this.warnData.timePicker[0]).getHours() + 1)+':'+ new Date(this.warnData.timePicker[0]).getMinutes() + ':' + new Date(this.warnData.timePicker[0]).getSeconds();
          warnEndTime = (new Date(this.warnData.timePicker[1]).getHours() + 1)+':'+ new Date(this.warnData.timePicker[1]).getMinutes() + ':' + new Date(this.warnData.timePicker[1]).getSeconds();
        }
        param.warnBeginTime = warnBeginTime;
        param.warnEndTime = warnEndTime;
        param.warnWeekend = parseInt(this.warnData.weekend);
        param.wechatPushSwitch = parseInt(this.warnData.chat);
        this.$Api.warnSetUpdate(param).then(res => {
            console.log(res);
            // if(res.code == '200'){
            //   this.refreshAside()
            // }
        })
      },
      handleCheckAllChange(val) {
        console.log(val);
        if(val){
          this.warnData.checkedTypeResource = [];
          for (let i = 0; i < this.warnSetData.typeResourceList.length; i++) {
            this.warnData.checkedTypeResource.push(this.warnSetData.typeResourceList[i].value)
          }
          this.warnData.isIndeterminate = false;
        }else{
          this.warnData.checkedTypeResource = [];
        }
      },
      handleCheckedChange(value) {
        console.log(value)
        let checkedCount = value.length;
        this.warnData.checkAll = checkedCount === this.warnSetData.typeResourceList.length;
        this.warnData.isIndeterminate = checkedCount > 0 && checkedCount < this.warnSetData.typeResourceList.length;
      }
  }
};
</script>
